#pragma once

#include "Serializable.h"
#include "ServiceProvider.h"
#include "LeagueLayer.h"
#include "Vectors.h"

struct LeagueManagerSettings : public Serializable
{
	virtual	ZStream& Serialize(ZStream& o_stream) const;
	virtual ZStream& Deserialize(ZStream& i_stream);

	LMSettingsContainer m_layerSettings;
	///... other future settings...
};

class LeagueManager : public Serializable
{
public:
	explicit LeagueManager(ServiceProvider* i_sp);
	~LeagueManager();

	void Initialize(const LeagueManagerSettings& i_settings);
	void Clear();

	void RestartLeagues();

	void Update(const bool i_doNotPrompt);
	void EndSeasonUpdate(const bool i_bDoNotPrompt, STD::stringstream& o_message);
	void HandleBankrupt(Team* const io_bankruptTeam);

	virtual	ZStream&		Serialize(ZStream& o_stream) const;
	virtual ZStream&		Deserialize(ZStream& i_stream);

	const League* const		GetLeagueFromID(const int i_id) const {return m_allLeagues.at(i_id);}
	LayerVec&				GetAllLayers();
	LeagueVec&				GetLeagues(bool onlyVisible);
	const LeagueVec&		GetLeagues(bool onlyVisible) const;
	size_t					GetNumOfLeagues(bool onlyVisible) const;
	size_t					GetNumOfTeams(bool onlyVisible) const;
	size_t					GetNumOfLayers(bool onlyVisible) const;

	void					GetTeamMatches(const int i_teamID, MatchDataVec& o_matches) const;

	bool					IsEnded() const;
	size_t					GetCurrentTurn() const;
	size_t					GetCurrentSeason() const;
	
	LeagueManagerSettings&	GetSettings();

private:
	bool CheckConsistency(const LeagueManagerSettings& i_settings);

	ServiceProvider* m_sp;

	LayerVec m_layers;
	LeagueManagerSettings m_settings;
	LeagueVec m_allVisibleLeagues;
	LeagueVec m_allLeagues;
	size_t m_currentTurn;
	size_t m_currentSeason;
};

inline LeagueVec& LeagueManager::GetLeagues(bool onlyVisible)
{
	if (onlyVisible)
		return m_allVisibleLeagues;
	else
		return m_allLeagues;
}

inline const LeagueVec& LeagueManager::GetLeagues(bool onlyVisible) const
{
	if (onlyVisible)
		return m_allVisibleLeagues;
	else
		return m_allLeagues;
}

inline size_t LeagueManager::GetNumOfLeagues(bool onlyVisible) const
{
	if (onlyVisible)
		return m_allVisibleLeagues.size();
	else
		return m_allLeagues.size();
}

inline LayerVec& LeagueManager::GetAllLayers()
{
	return m_layers;
}

inline LeagueManagerSettings& LeagueManager::GetSettings()
{
	return m_settings;
}

inline size_t LeagueManager::GetCurrentTurn() const
{
	return m_currentTurn;
}

inline size_t LeagueManager::GetCurrentSeason() const
{
	return m_currentSeason;
}
